<template>
  <div class="app-container">
    <!--查询表单-->
<el-form :inline="true" :model="serchObj" class="demo-form-inline">
    <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称" @keyup.enter.native="fetchData()"/>
    </el-form-item>

    <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号" @keyup.enter.native="fetchData()"/>
    </el-form-item>

    <el-form-item>
    <el-button type="primary"  @click="fetchData()">查询</el-button>
    </el-form-item>

    <el-form-item> 
    <el-button type="default" @click="resetData()">清空</el-button>
    </el-form-item>
    
    <el-form-item> 
    <el-button type="danger" @click="batchRemove()">批量删除</el-button>
    </el-form-item>

</el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="selectionChange"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
    >
       <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (page - 1) * size + scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column prop="hosname" label="医院名称" width="180" />

        <el-table-column prop="hoscode" label="医院编号" width="110" />

        <el-table-column prop="apiUrl" label="地址" width="200" />

        <el-table-column prop="contactsName" label="联系人" />

        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? "可用" : "不可用" }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <router-link :to="'/yygh/hospset/edit/' + scope.row.id">
              <el-button type="primary" size="mini" icon="el-icon-edit"
                >修改</el-button
              >
            </router-link>
            <el-button v-if="scope.row.status==1"
              type="warning"
              size="mini"
              icon="el-icon-lock"
              @click="lock(scope.row.id,0)">锁定</el-button>

              <el-button v-else
              type="primary"
              size="mini"
              icon="el-icon-unlock"
              @click="lock(scope.row.id,1)">解锁</el-button>

              <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="removeDataById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="size"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>

<script>
import hosp from "@/api/hosp";

export default {
  data() {
    //定义数据
    return {
      listLoading: false, //加载层
      list: [], //数据列表
      total: 0, //总行数
      page: 1,
      size: 5,
      searchObj: {}, //查询条件
      idList:[],
    };
  },
  created() {
    //钩子函数
    this.fetchData(1);
  },
  methods: {
     //解锁与锁定
     lock(id,status){
        hosp.lockById(id,status).then((response)=>{
            this.$message.success(status==1?"解锁成功":"锁定成功");
            this.fetchData(this.page);
          });   
     },
     //切换多选框
     selectionChange(rows){
      //每次添加是清空
      this.idList=[];
      //获取选中行的id
      for(let i=0;i<rows.length;i++){
        this.idList.push(rows[i].id);
      }

     } ,
    //批量删除
    batchRemove(){
         this.$confirm("确定删除改数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })      
      .then(()=>{        
          hosp.batchRemove(this.idList).then((response)=>{
            this.$message.success("批量删除成功");
            this.fetchData();
          });   
      })
       .catch(() => {});
         
    },
    //根据id 删除
    removeDataById(id) {
      this.$confirm("确定删除改数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //判断当前页面是否只有一行
          let length = document.getElementsByClassName("el-table_row").length;
          if (length == 1) {
            this.page = this.page - 1;
          }
          hosp.removeById(id).then((reponse) => {
            this.$message.success("删除成功");
            this.fetchData(this.page);
          });
        })
        .catch(() => {});
    },

    //清空查询条件
    resetData() {
      this.searchObj = {};
      this.fetchData();
    },
    //查询列表
    fetchData(page = 1) {
      this.page = page;
      this.listLoading = true;
      hosp.getList(this.page, this.size, this.searchObj).then((response) => {
        this.list = response.data.rows;
        this.total = response.data.total;

        this.listLoading = false;
      });
    },
  },
};
</script>